//饼状图
var Echart3NormalPie = function(chartid){
	var title = "";
	var myChart = echarts.init(document.getElementById(chartid));
	var legendLabels = [];
	var dataNameValues = [];
	
	var getOption = function(){
		return {
			  title:{
				  text:title
			  },
			    tooltip : {
			        trigger: 'item',
			        formatter: "{a} <br/>{b} : {c} ({d}%)"
			    },
			    toolbox : {
					show : true,
					iconStyle : {
						normal : {},
						emphasis : {}
					},
					feature : {
						dataView : {
							readOnly : true,
							optionToContent : function(opt) {
								var series = opt.series;
								var coldata1 = [];
								var coldatas = [];
								for ( var j = 0; j < series[0].data.length; j++) {
									coldata1.push(series[0].data[j].name);
								}
								for ( var j = 0; j < series.length; j++) {
									var ser = series[j];
									var col = [];
									for ( var i = 0; i < ser.data.length; i++) {
										col.push(ser.data[i].value);
									}
									coldatas.push(col);
								}
								var table = '<table border="1" style="width:100%;text-align:center"><tbody><tr>'
										+ '<td>' + '</td>';
								for ( var j = 0; j < series.length; j++) {
									if (series[j].name) {
										table = table + '<td>' + series[j].name
												+ '</td>';
									} else {
										table = table + '<td>' + " " + '</td>';
									}
								}
								for ( var i = 0, l = coldata1.length; i < l; i++) {
									table += '<tr>' + '<td>' + coldata1[i]
											+ '</td>';
									for ( var z = 0; z < coldatas.length; z++) {
										if (isNaN(parseInt(coldatas[z][i]))) {
											table = table + '<td>' + '</td>';
										} else {
											table = table + '<td>' + coldatas[z][i]
													+ '</td>';
										}
									}
								}
								table += '</tbody></table>';
								return table;
							},

							iconStyle : {
								normal : {},
								emphasis : {}
							}
						},
						saveAsImage : {
							iconStyle : {
								normal : {},
								emphasis : {}
							}
						}
					}
				},
			    legend: {
			        orient: 'vertical',
			        left: 'left',
			        data:legendLabels
			    },
			    series : [
			        {
			            name: '',
			            type: 'pie',
			            label:{
							normal:{
								show:true,
				                formatter: "{b}:{d}%",
								textStyle:{}
							}
					    },
			            radius : '55%',
			            center: ['60%', '60%'],
			            data:dataNameValues,
			            itemStyle: {
			                emphasis: {
			                    shadowBlur: 10,
			                    shadowOffsetX: 0,
			                    shadowColor: 'rgba(0, 0, 0, 0.5)'
			                }
			            }
			        }
			    ]
		};
	};
	this.setTitle = function(name){
		title = name;
	};
	this.setLegendLabels = function(datas){
		legendLabels = datas;
	};
	this.setDataNameValues = function(datas){
		dataNameValues = datas;
	};
	this.addDataNameValue = function (pname,pvalue){
		dataNameValues.push({value:pvalue,name:pname});
	} ;
	this.show = function(){
		myChart.setOption(getOption());
	};
	this.getChart = function(){
		return myChart;
	};
};
//柱状图和折线图
var EchartsLineBar=function(chartid){
	var colors = ['#5793f3', '#FF4500', '#675bba'];
	var title='';	
	var yleftname='';
	var yrightname='';
	var legendLabels = [];
	var ratio=[];
	var number=[];
	var years=[];
	var myChart = echarts.init(document.getElementById(chartid));	
	var getOption =function() {
	return {

		    title: {
		        text:title,
		        subtext:"点击查看该年发电机故障详情",
		        left: 'center'
		    },
		    tooltip: {
		        trigger: 'axis'
		    },
		    toolbox: {
		        feature: {
		        	dataView: {
                    	readOnly: false,
                    	optionToContent:function(opt){
                    		var axisData = opt.xAxis[0].data;
                    	    var series = opt.series;
                    	    var table = '<table border="1" style="width:100%;text-align:center"><tbody><tr>'
                    	                 + '<td>'+opt.xAxis[0].name+'</td>';
                    	    for(var j=0;j<series.length;j++){
                    	    	table = table + '<td>' + series[j].name + '</td>';
                    	    }
                    	    for (var i = 0, l = axisData.length; i < l; i++) {
                    	        table += '<tr>'
                    	                 + '<td>' + axisData[i] + '</td>';
                    	        for(var z=0;z<series.length;z++){
                    	        	if(isNaN(parseInt(series[z].data[i]))){
                    	        		table = table + '<td>' + '</td>';
                    	        	}else{
                    	        		table = table + '<td>' + series[z].data[i] + '</td>';
                    	        	}
                    	        }
                    	    }
                    	    table += '</tbody></table>';
                    	    return table;
                    	},
                    	iconStyle:{
                    		normal:{},
                    		emphasis:{}
                    	}
                    },
		            restore: {
		                show: true
		            },
		            saveAsImage: {
		                show: true
		            }
		        }
		    },
		    grid: {
		        containLabel: true
		    },
		    legend: {
		        data: legendLabels
		    },
		    xAxis: [{
		    	name:'',
		        type: 'category',
		        axisTick: {
		            alignWithLabel: true
		        },
		        data: years
		    }],
		    yAxis: [{
		        type: 'value',
		        name: yrightname,
		        position: 'right',
		        axisLabel: {
		            formatter: '{value} %',			           
		        }
		    }, {
		        type: 'value',
		        name: yleftname,
		        position: 'left',
		    }],
		    series: [{
		        name: yrightname,
		        type: 'line',
		        stack: '总量',
		        smooth:false,
		        label: {
		            normal: {
		                show: true,
		                position: 'top',
		            }
		        },
		        lineStyle: {
		            normal: {
		                width: 3,
		                shadowColor: 'rgba(0,0,0,4)',
		                shadowBlur: 10,
		                shadowOffsetY: 10
		            },
		        },
		        itemStyle: {
		        		              normal: {
		        		                  color:colors[2]
		        		              }
		        		        },
		        data: ratio
		    }, {
		        name: yleftname,
		        type: 'bar',
		        yAxisIndex: 1,
		        stack: '总量',
		        label: {
		            normal: {
		                show: true,
		                position: 'inside'
		            }
		        },
		        data: number
		    }]
		};
		
	};
	this.setTitle = function(name){
		title = name;
	};
	this.setYleftname = function(name){
		yleftname = name;
	};
	this.setYrightname = function(name){
		yrightname = name;
	};
	this.setLegendLabels = function(datas){
		legendLabels = datas;
	};
	this.setRatio = function(datas){
		ratio = datas;
	};
	this.setNumber = function(datas){
		number = datas;
	};
	this.setYears = function(datas){
		years = datas;
	};
	this.show = function(){
		myChart.setOption(getOption());
		return this;
	};
	this.getChart = function(){
		return myChart;
	};
};
//双柱状图加折线图
var Echarts3LineBars=function(chartid){
	var colors = ['#C25652', '#51616D', '#675bba'];
	///var colors = ['red', '#008B8B', '#675bba'];
	var myChart = echarts.init(document.getElementById(chartid));	
	var option =
		 {
			color: colors,
		    tooltip: {
		        trigger: 'axis'
		    },
		    grid: {
		        right: '20%'
		    },
		    toolbox: {
		        feature: {
		           dataView: {
                    	readOnly: false,
                    	optionToContent:function(opt){
                    		var axisData = opt.xAxis[0].data;
                    	    var series = opt.series;
                    	    var table = '<table border="1" style="width:100%;text-align:center"><tbody><tr>'
                    	                 + '<td>'+opt.xAxis[0].name+'</td>';
                    	    for(var j=0;j<series.length;j++){
                    	    	table = table + '<td>' + series[j].name + '</td>';
                    	    }
                    	    for (var i = 0, l = axisData.length; i < l; i++) {
                    	        table += '<tr>'
                    	                 + '<td>' + axisData[i] + '</td>';
                    	        for(var z=0;z<series.length;z++){
                    	        	if(isNaN(parseInt(series[z].data[i]))){
                    	        		table = table + '<td>' + '</td>';
                    	        	}else{
                    	        		table = table + '<td>' + series[z].data[i] + '</td>';
                    	        	}
                    	        }
                    	    }
                    	    table += '</tbody></table>';
                    	    return table;
                    	},
                    	iconStyle:{
                    		normal:{},
                    		emphasis:{}
                    	}
                    },
		            saveAsImage: {show: true}
		        }
		    },
		    legend: {
		        data:[]
		    },
		    xAxis: [
		        {
		            type: 'category',
		            axisTick: {
		                alignWithLabel: true
		            },
		            axisPointer:{
				    	type:'shadow',
						label:{
							textStyle:{}
						},
						lineStyle:{},
						shadowStyle:{},
						handle:{},
						crossStyle:{}
					},
		            data: ''
		        }
		    ],
		    yAxis: [
		        {
		            type: 'value',
		            name: '',
		            position: 'left',		     
		            axisLabel: {
		                formatter: '{value} '
		            }
		        },
		        {
		            type: 'value',
		            name: '',
		            position: 'right',
		            axisLabel: {
		                formatter: '{value} '
		            }
		        }
		    ],
		    series: [
		       
		        {
		            name:'',
		            type:'bar',
		            yAxisIndex: 0,
		            label: {
			            normal: {
			                show: true,
			                position: 'top',
			            }
			        },
		            data:[]
		        },
		        {
		            name:'',
		            type:'bar',
		            yAxisIndex: 0,
		            //barGap:'-100%',
		            label: {
			            normal: {
			                show: true,
			                position: 'top',
			            }
			        },
//			        itemStyle:{normal:{opacity:0.4}},
		            data:[]
		        },
		        {
		            name:'',
		            type:'line',
		            yAxisIndex: 1,
		            label: {
			            normal: {
			                show: true,
			                textStyle:{
			                	color:'#4B0082'
			                }
			            }
			        },
			        lineStyle:{
			        	normal:{
			        		color:'#4B0082'
			        	}
			        },
		            data:[]
		        }
		    ]
			
			
		};
	this.getOption=function(){
		return option;
	};
	
	this.setLeftyAxisTitle = function(title){
		option.yAxis[0].name = title;
		return this;
	};
	this.setRightyAxisTitle = function(title){
		option.yAxis[1].name = title;
		return this;
	};
	this.setYleftname = function(name){
		option.series[0].name = name;
	};
	this.setYrightname = function(name){
		option.series[2].name = name;
	};
	this.setYaddname = function(name){
		option.series[1].name = name;
	};
	this.setLegendLabels = function(datas){
		option.legend.data = datas;
	};
	this.setRatio = function(datas){
		option.series[2].data = datas;
	};
	this.setNumber = function(datas){
		option.series[0].data = datas;
	};
	this.setYears = function(datas){
		option.xAxis[0].data = datas;
	};
	this.setAdd = function(datas){
		option.series[1].data = datas;
	};
	this.show = function(){
		myChart.setOption(option);
	};
	this.getChart = function(){
		return myChart;
	};
};
